package com.lenovo.smarttraffic.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.Item1IntentActivity;
import com.lenovo.smarttraffic.ui.fragment.fgBean.AdapterBean;
import com.lenovo.smarttraffic.ui.fragment.fgBean.JsonBean;
import com.lenovo.smarttraffic.ui.fragment.fgBean.NewBean;
import com.lenovo.smarttraffic.ui.fragment.fgBean.RetrofitPostApi1;
import com.lenovo.smarttraffic.ui.fragment.fragmentAdapter.Fg1Adapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 没用
 *
 */
public class SecondFragment extends BaseFragment {
    private ListView lv_fg2;
    private SwipeRefreshLayout swipeRefreshLayout_fg2;
    private String firsttab;
    private List<NewBean.ROWSDETAILBean> newslist;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    List<NewBean.ROWSDETAILBean> obj1 = (List<NewBean.ROWSDETAILBean>) msg.obj;
                    for (int i = 0; i < obj1.size(); i++) {
                        adapterBeanList.add(new AdapterBean(obj1.get(i).getTitle(), obj1.get(i).getCreatetime(), obj1.get(i).getContent(), "w"));
                    }
                    swipeRefreshLayout_fg2.setRefreshing(false);//关闭刷新
                    break;
                case 2:
                    swipeRefreshLayout_fg2.setRefreshing(false);//关闭刷新
                    refreshPage(null);
                    break;
            }
            Collections.sort(adapterBeanList, new Comparator<AdapterBean>() {
                @Override
                public int compare(AdapterBean t0, AdapterBean t1) {
                    return (t0.getTime().compareTo(t1.getTime()));
                }
            });
            lv_fg2.setAdapter(new Fg1Adapter(mContext, adapterBeanList));
            lv_fg2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent(mContext, Item1IntentActivity.class);
                    Bundle bundle = new Bundle();
                    AdapterBean adapterBean = adapterBeanList.get(i);
                    bundle.putSerializable("fg1", (Serializable) adapterBean);
                    intent.putExtras(bundle);
                    intent.putExtra("classify", firsttab);
                    startActivity(intent);
                }
            });
        }
    };
    private List<AdapterBean> adapterBeanList;

    @Override
    protected View getSuccessView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            firsttab = bundle.getString("SecondFragment");
        } else {
            firsttab = "";
        }
        View view = View.inflate(getActivity(), R.layout.fragment_second, null);
        initView(view);
        initRefresh();
        return view;
    }

    private void initView(View view) {
        swipeRefreshLayout_fg2 = view.findViewById(R.id.swipeRefreshLayout_fg2);
        lv_fg2 = view.findViewById(R.id.lv_fg2);
        adapterBeanList = new ArrayList<>();
    }

    private void initRefresh() {
        //下拉刷新的圆圈是否显示
        swipeRefreshLayout_fg2.setRefreshing(false);
        //设置下拉时圆圈的颜色（可以由多种颜色拼成）
        swipeRefreshLayout_fg2.setColorSchemeResources(
                android.R.color.holo_blue_bright
        );
        //设置下拉时圆圈的背景颜色（这里设置成白色）
        swipeRefreshLayout_fg2.setProgressBackgroundColorSchemeResource(
                android.R.color.white
        );
        //设置下拉刷新时的操作
        swipeRefreshLayout_fg2.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                retrofitRefresh();
            }
        });
    }

    @Override
    protected Object requestData() {
        SystemClock.sleep(1000);/*模拟请求服务器的延时过程*/
        return Constant.STATE_FAILED;/*加载为空*/
    }

    private void retrofitRefresh() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.110:8088/transportservice/action/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitPostApi1 retrofitPostApi1 = retrofit.create(RetrofitPostApi1.class);
        Call<NewBean> call1 = retrofitPostApi1.getCall(new JsonBean("user1"));
        call1.enqueue(new Callback<NewBean>() {
            @Override
            public void onResponse(Call<NewBean> call, Response<NewBean> response) {
                newslist = response.body().getROWS_DETAIL();
                Message message = new Message();
                message.what = 1;
                message.obj = newslist;
                handler.sendMessageDelayed(message, 1000);
            }

            @Override
            public void onFailure(Call<NewBean> call, Throwable t) {
                Message message = new Message();
                message.what = 2;
                message.obj = null;
                handler.sendMessageDelayed(message, 1000);
            }
        });
    }

    @Override
    public void onClick(View view) {

    }

    public static SecondFragment onNewBundle(String args) {
        SecondFragment secondFragment = new SecondFragment();
        Bundle bundle = new Bundle();
        bundle.putString("SecondFragment", args);
        secondFragment.setArguments(bundle);
        return secondFragment;
    }


}
